package com.bl.student.api.controller.user;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.service.biz.address.AddressService;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.address.AddressAddVo;
import com.bl.student.service.vo.address.AddressModVo;
import com.bl.student.service.vo.address.AddressVo;
import com.bl.student.service.vo.information.MyPageVo;
import com.bl.student.service.vo.tag.TagListVo;
import com.bl.student.service.vo.user.UserInfoModifyVo;
import com.bl.student.service.vo.user.UserInviteVo;
import com.bl.student.service.vo.user.UserSimpleInfoVo;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by yujingyi on 2018/1/6.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis/user"})
@Api(value = "用户相关api", description = "用户相关api")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取当前用户信息接口", notes = "获取当前用户信息接口")
    @ApiImplicitParams({
    })
    @GetMapping(value = {"/my_info"})
    public ApiResponseBody<UserSimpleInfoVo> userInfo(
            @RequestParam(value = "type", required = true) String type,
            HttpServletRequest request) {

        UserSimpleInfoVo userSimpleInfoVo = userService.getUserById(getUserInfo(request).getId(), type);

        return ApiResponseBody.createSuccessBody(userSimpleInfoVo);
    }


    @ApiOperation(value = "用户信息修改接口", notes = "")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/modify"})
    @ResponseBody
    public ApiResponseBody<UserInfoModifyVo> modify(
            @Valid @RequestBody UserInfoModifyVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        userService.modifyUser(body, getUserInfo(request));

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "获取用户邀请列表", notes = "获取用户邀请列表接口")
    @ApiImplicitParams({
    })
    @GetMapping(value = {"/invite_list"})
    public ApiResponseBody<UserInviteVo> inviteList(
            HttpServletRequest request) {

        UserInviteVo userInviteVo = userService.getUserInviteList(getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(userInviteVo);
    }

    @ApiOperation(value = "获取标签列表", notes = "获取用户邀请列表接口")
    @ApiImplicitParams({
    })
    @GetMapping(value = {"/tag_list"})
    public ApiResponseBody<List<String>> tagList(
            HttpServletRequest request) {

        List<String> userInviteVo = userService.getUserTag(getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(userInviteVo);
    }

    @ApiOperation(value = "删除单个用户标签 id传标签名", notes = "删除单个用户标签 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_tag"})
    public ApiResponseBody delTag(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {

        userService.delTag(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "增加标签", notes = "增加地址信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_tag"})
    public ApiResponseBody add_tag(
            @Valid @RequestBody String body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        userService.addTag(body, getUserInfo(request).getId());
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改用户标签列表", notes = "修改用户标签列表 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/mod_tag_list"})
    public ApiResponseBody mod_tag(
            @Valid @RequestBody List<String> body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        userService.modTagList(body, getUserInfo(request).getId());
        return ApiResponseBody.createSuccessBody(null);
    }

    @Autowired
    AddressService addressService;

    @ApiOperation(value = "增加地址信息", notes = "增加地址信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_address"})
    public ApiResponseBody add(
            @Valid @RequestBody AddressAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        addressService.add(body, getUserInfo(request).getId());
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "修改地址信息", notes = "修改地址信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify_address"})
    public ApiResponseBody modifyAddress(
            @Valid @RequestBody AddressModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        addressService.modify(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "查询用户地址信息", notes = "查询地址信息 ")
    @ApiImplicitParams({})
    @GetMapping(value = {"/get_address_detail"})
    public ApiResponseBody<AddressVo> getAddressVo(
            HttpServletRequest request) {

        AddressVo addressVo = addressService.getAddressVo(getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(addressVo);
    }


    @ApiOperation(value = "删除用户地址", notes = "删除用户地址 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_address"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {

        addressService.del(body);

        return ApiResponseBody.createSuccessBody(null);
    }

}
